﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight;
using HandyControl.Data;

namespace chatGUI.Model
{
    public enum chatBubbleRole
    {
        sender, reciever
    }
    public enum chatBubbleType
    {
        text, gif, picture, file
    }
    public class chatBubbleModel : ObservableObject
    {
        private string id;
        public string Id
        {
            get => id;
            set => Set(ref id, value);
        }
        public string UserId
        {
            get => "User" + id;
        }
        private string senderNickName;
        public string SenderNickName
        {
            get => senderNickName;
            set => Set(ref senderNickName, value);
        }
        private string senderAvatarUrlStr;
        public string SenderAvatarUrlStr
        {
            get => senderAvatarUrlStr;
            set => Set(ref senderAvatarUrlStr, value);
        }
        private DateTime sendTime;
        public DateTime SendTime
        {
            get => sendTime;
            set => Set(ref sendTime, value);
        }
        public string SendTimeStr
        {
            get => getSendTimeStr(SendTime);
        } 
        private Visibility timeVisiblity;
        public Visibility TimeVisiblity
        {
            get => timeVisiblity;
            set => Set(ref timeVisiblity, value);
        }
        private object message;
        public object Message
        {
            get => message;
            set => Set(ref message, value);
        }
        private chatBubbleRole chatBubbleRole;
        public chatBubbleRole ChatBubbleRole
        {
            get => chatBubbleRole;
            set => Set(ref chatBubbleRole, value);
        }
        private chatBubbleType chatBubbleType;
        public chatBubbleType ChatBubbleType
        {
            get => chatBubbleType;
            set => Set(ref chatBubbleType, value);
        }
        public bool IsText
        {
            get => ChatBubbleType == chatBubbleType.text ? true : false;
        }
        private static string getSendTimeStr(DateTime dateTime)
        {
            if(dateTime < DateTime.Now.AddHours(-12))
            {
                return dateTime.ToString("yyyy/MM/dd HH:mm:ss");
            }
            return dateTime.ToString("HH:mm:ss");
        }
        private fileInfoModel fileInfoModel;
        public fileInfoModel FileInfoModel
        {
            get => fileInfoModel;
            set => Set(ref fileInfoModel, value);
        }
    }
    public class fileInfoModel
    {
        public string fileName { get; set; }
        public string fileIcon { get; set; }
        public string fileSize { get; set; }
        public string fileStr
        {
            get => fileSize==string.Empty? $"{fileName}": $"{fileName} ({fileSize})";
        }
        public fileInfoModel(string fileName, long fileLength)
        {
            this.fileName = fileName;
            fileSize = getFileSize(fileLength);
            fileIcon = getFileIcon(fileName.Substring(fileName.LastIndexOf(".")));
        }
        public fileInfoModel(string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);
            fileSize = getFileSize(fileInfo.Length);
            fileName = fileInfo.Name;
            fileIcon = getFileIcon(fileName.Substring(fileName.LastIndexOf(".")));
        }
        public fileInfoModel(string fileName, bool nouse)
        {
            this.fileName = fileName;
            fileSize = string.Empty;
            fileIcon = getFileIcon(fileName.Substring(fileName.LastIndexOf(".")));
        }
        public fileInfoModel()
        {
            fileName = string.Empty;
            fileSize = string.Empty;
            fileIcon = string.Empty;
        }
        private string getFileSize(long length)
        {
            double byteNum = Convert.ToDouble(length) / 1024;
            if(byteNum > 1024)
            {
                byteNum /= 1024;
                if(byteNum > 1024)
                {
                    byteNum /= 1024;
                    string len = byteNum.ToString();
                    return $"{len.Substring(0, len.Length > 2 ? 3 : len.Length)} GB";
                }
                else
                {
                    string len = byteNum.ToString();
                    return $"{len.Substring(0, len.Length > 2 ? 3 : len.Length)} MB";
                }
            }
            else
            {
                string len = byteNum.ToString();
                return $"{len.Substring(0, len.Length > 2 ? 3 : len.Length)} KB";
            }
        }
        private string getFileIcon(string suffix)
        {
            if(suffix == null)
            {
                return "../resources/image/broken.png";
            }
            else if(suffix == ".txt")
            {
                return "../resources/image/text.png";
            }
            else if(suffix == ".exe")
            {
                return "../resources/image/exe.png";
            }
            else if(suffix == ".png" || suffix == ".jpg" || suffix == ".jpeg" || suffix == ".gif")
            {
                return "../resources/image/picture.png";
            }
            else if (suffix == ".mp4" || suffix == ".mpeg" || suffix == ".flv")
            {
                return "../resources/image/video.png";
            }
            else if (suffix == ".pdf")
            {
                return "../resources/image/pdf.png";
            }
            else if (suffix == ".docx")
            {
                return "../resources/image/word.png";
            }
            else if (suffix == ".execl")
            {
                return "../resources/image/execl.png";
            }
            else
            {
                return "../resources/image/unknown.png";
            }
        }
    }
}
